package com.symbio.ishenapi.background.controller;



import com.alibaba.fastjson.JSONArray;
import com.symbio.ishenapi.background.model.TmHospitalInfo;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 医院列表
 */
@RequestMapping("/tmHospitalInfoController")
public class TmHospitalInfoController extends BaseController {

    /**
     * 查询全部医院信息
     */
    public void findAll(){
        String hospitalName = getPara("hospitalName");
        Map map = TmHospitalInfo.dao.findAll(hospitalName,getQueryModel());
        renderPageJsonRet(map.get("count"),map.get("data"));
    }

    /**
     * 保存医院
     */
    public void save(){
        TmHospitalInfo user = (TmHospitalInfo)getReqBodyObject(TmHospitalInfo.class);
        if(user.getHospitalName() == null || "".equals(user.getHospitalName())){
            renderError(40003,"医院名称");
            return;
        }
        if(user.getHospitalDescribe() == null || "".equals(user.getHospitalDescribe())){
            renderError(40003,"医院描述");
            return;
        }
        user.setCreateTime(new Date());
        user.setStatus(1);
        if (user.save()){
            renderJsonRet(40001);
        }else {
            renderError(40002,"医院");
            return;
        }
    }


    /**
     * 修改删除字段，单条
     */
    public void updateHospital(){
        TmHospitalInfo user = (TmHospitalInfo)getReqBodyObject(TmHospitalInfo.class);
       TmHospitalInfo tmHospitalInfo = TmHospitalInfo.dao.findById(user.getId());
        tmHospitalInfo.setStatus(user.getStatus());
        if (tmHospitalInfo.update()){
            renderJsonRet(40001);
        }else {
            renderError(40002);
            return;
        }
    }



    /**
     * 修改删除字段，批量
     */
    public void updateHospitalList(){
       // TmHospitalInfo user = (TmHospitalInfo)getReqBodyObject(TmHospitalInfo.class);
        String idse = getReqBodyString().replace("\"","").replace("\"","");
        List<TmHospitalInfo> tmHospitalInfoList = TmHospitalInfo.dao.findByIdList(idse);
       for(TmHospitalInfo tmHospitalInfo : tmHospitalInfoList){
           tmHospitalInfo.setStatus(0);
           if (tmHospitalInfo.update()){
               renderJsonRet(40001);
           }else {
               renderError(40002);
               return;
           }
       }
    }


    /**
     * 根据医院id获取医院的医生
     */
    public void findHosId(){
        String hosId = getPara("hosId");
        Map map = TmHospitalInfo.dao.findList(getQueryModel(),hosId);
        renderPageJsonRet(map.get("count"),map.get("data"));
    }


    /**
     *
     * 外呼通话接口，飞
     *
     */
    public void Conversation(){
    /*public static void main(String[] args) {*/
        String account = "N00000033612";//替换为您的账户
        String secret = "dfbc95e0-e266-11e8-8924-993e2051895b";//替换为您的api密码
        String host = "http://121.41.90.152";
        Log logger = LogFactory.getLog(TmHospitalInfoController.class);


            String time = getDateTime();
            String sig = md5(account + secret + time);
            //查询坐席状态接口
            String interfacePath = "/v20160818/call/dialout/";
            String url = host + interfacePath + account + "?sig=" + sig;
            String auth = base64(account + ":" + time);
            HttpClientBuilder builder = HttpClientBuilder.create();
            CloseableHttpClient client = builder.build();
            HttpPost post = new HttpPost(url);
            post.addHeader("Accept", "application/json");
            post.addHeader("Content-Type","application/json;charset=utf-8");
           // post.addHeader("Authorization",auth);

            StringEntity requestEntity = null;
            //根据需要发送的数据做相应替换
            requestEntity = new StringEntity("{\"FromExten\":\"8000\",\"Exten\":\"18514533541\",\"ExtenType\":\"Local\"}","UTF-8");
            post.setEntity(requestEntity);
            CloseableHttpResponse response = null;
            try {
                response = client.execute(post);
                HttpEntity entity = response.getEntity();
                logger.info("the response is : " + EntityUtils.toString(entity,"utf8"));

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (response != null){
                    try {
                        response.close();
                        renderError(40001,"外呼成功");
                        return;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    }
    public static String md5 (String text) {
        return DigestUtils.md5Hex(text).toUpperCase();
    }
    public static String base64 (String text) {
        byte[] b = text.getBytes();
        Base64 base64 = new Base64();
        b = base64.encode(b);
        String s = new String(b);
        return s;
    }
    public static String getDateTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }


    /**
     *
     * 修改坐席手机号，飞
     *
     */
    public void updateMobile() {

        /* public static void main(String[] args) {*/
        String exten = getPara("exten");//坐席号
        String mobile = getPara("mobile");//手机号

        String account = "N00000033612";//替换为您的账户
        String secret = "dfbc95e0-e266-11e8-8924-993e2051895b";//替换为您的api密码
        String host = "http://121.41.90.152";
        Log logger = LogFactory.getLog(TmHospitalInfoController.class);

        String time = getDateTime();
        String sig = md5(account + secret + time);
        //修改坐席手机号接口
        String interfacePath = "/v20160818/user/updateUser/";
        String url = host + interfacePath + account + "?sig=" + sig;
        String auth = base64(account + ":" + time);
        HttpClientBuilder builder = HttpClientBuilder.create();
        CloseableHttpClient client = builder.build();
        System.out.println(url);
        HttpPost post = new HttpPost(url);
        post.addHeader("Accept", "application/json");
        post.addHeader("Content-Type", "application/json;charset=utf-8");
        post.addHeader("Authorization", auth);
        StringEntity requestEntity = null;
        //根据需要发送的数据做相应替换
        requestEntity = new StringEntity("{\"exten\":"+exten+",\"mobile\":"+mobile+"}", "UTF-8");
        post.setEntity(requestEntity);
        CloseableHttpResponse response = null;
        try {
            response = client.execute(post);
            HttpEntity entity = response.getEntity();
            logger.info("the response is : " + EntityUtils.toString(entity, "utf8"));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                    // renderError(40001,"修改坐席手机号成功");
                    return;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) {

        String account = "N00000033612";//替换为您的账户
        String secret = "dfbc95e0-e266-11e8-8924-993e2051895b";//替换为您的api密码
        String host = "http://apis.7moor.com/v20170704/cdr/getCCCdr/";
        Log logger = LogFactory.getLog(TmHospitalInfoController.class);

        String time = getDateTime();
        String sig = md5(account + secret + time);
        //修改坐席手机号接口
        String interfacePath = "/v20160818/user/updateUser/";
        String url = host + account + "?sig=" + sig;
        String auth = base64(account + ":" + time);
        HttpClientBuilder builder = HttpClientBuilder.create();
        CloseableHttpClient client = builder.build();
        System.out.println(url);
        HttpPost post = new HttpPost(url);
        post.addHeader("Accept", "application/json");
        post.addHeader("Content-Type","application/json;charset=utf-8");
        post.addHeader("Authorization",auth);
        //post.addHeader("ACCOUNTID",account);
        /*post.addHeader("beginTime","2018-01-04 00:00:00");
        post.addHeader("endTime","2018-1-05 10:15:00");*/
        StringEntity requestEntity = null;
        //根据需要发送的数据做相应替换
        requestEntity = new StringEntity("{\"beginTime\":\"2018-11-10 00:00:00\",\"endTime\":\"2018-11-15 10:15:00\"}","UTF-8");
        post.setEntity(requestEntity);
        CloseableHttpResponse response = null;
        try {
            response = client.execute(post);
            HttpEntity entity = response.getEntity();
            String str = EntityUtils.toString(entity,"utf8");
            System.out.println(str);
            JSONObject jsonObj1 = new JSONObject(str);
            System.out.println(jsonObj1);
            Object data = jsonObj1.get("data");
            System.out.println("33333="+data);
            JSONArray myJsonArray = JSONArray.parseArray(data.toString());
            System.out.println( myJsonArray);
           for(int i = 0;i < myJsonArray.size();i++){
               Object job = myJsonArray.get(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
               System.out.println(job);
           }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (response != null){
                try {
                    response.close();
                    // renderError(40001,"修改坐席手机号成功");
                    return;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }




}
